#include <gui/wifipage_screen/WiFiPageView.hpp>
//
#include <texts/TextKeysAndLanguages.hpp>
#include "BitmapDatabase.hpp"
//标准库
#include <stdio.h>
#include <string.h>
//
WiFiPageView::WiFiPageView()
{

}
//
void WiFiPageView::setupScreen()
{
    WiFiPageViewBase::setupScreen();
}
//
void WiFiPageView::tearDownScreen()
{
    WiFiPageViewBase::tearDownScreen();
}
//连接网络
void WiFiPageView::connectWiFi()
{
		presenter->WiFiLinkTask(true);	//启用WiFi读取RSSI任务
}
//字符与信号强度显示
void WiFiPageView::updateWiFiRSSI(uint8_t (&pWiFiInfo)[40], uint16_t newRSSI)
{
    this->TextAreaAddStr(pWiFiInfo, sizeof(pWiFiInfo), newRSSI);
}
//字符显示函数
void WiFiPageView::TextAreaAddStr(uint8_t* str, uint32_t len, uint16_t newRSSI)
{
    int16_t textHeight = 0, nowTextHeight = 0;
    nowTextHeight = textWiFiLinkInfo.getTextHeight();
    textHeight = textWiFiLinkInfo.getHeight();
		//文本显示满，清除
    if (nowTextHeight > textHeight)
    {
        memset(textBuf, 0, 10);
    }
    uint32_t lens = strlen((char*)textBuf);
    memcpy((char*)textBuf + lens, (char*)str, len);
    Unicode::fromUTF8(textBuf, textWiFiLinkInfoBuffer, lens + len);
    textWiFiLinkInfo.setWideTextAction(WIDE_TEXT_CHARWRAP);
    textWiFiLinkInfo.invalidate();
		//根据RSSI值更新图标
		if(newRSSI <= 40){
			WirelessConnection.setBitmaps(touchgfx::Bitmap(BITMAP_WIFI_RSSI_4_SIGNAL_ID),touchgfx::Bitmap(BITMAP_WIFILINK_PRESSED_37X26_ID));	
		} 
		else if(newRSSI > 40 && newRSSI <= 50){
			WirelessConnection.setBitmaps(touchgfx::Bitmap(BITMAP_WIFI_RSSI_3_SIGNAL_ID),touchgfx::Bitmap(BITMAP_WIFILINK_PRESSED_37X26_ID));	
		}
		else if(newRSSI > 50 && newRSSI <= 60){
			WirelessConnection.setBitmaps(touchgfx::Bitmap(BITMAP_WIFI_RSSI_2_SIGNAL_ID),touchgfx::Bitmap(BITMAP_WIFILINK_PRESSED_37X26_ID));	
		}
		else if(newRSSI > 70 && newRSSI <= 80){
			WirelessConnection.setBitmaps(touchgfx::Bitmap(BITMAP_WIFI_RSSI_1_SIGNAL_ID),touchgfx::Bitmap(BITMAP_WIFILINK_PRESSED_37X26_ID));	
		}
		else if(newRSSI > 80){
			WirelessConnection.setBitmaps(touchgfx::Bitmap(BITMAP_WIFI_RSSI_NO_SIGNAL_ID),touchgfx::Bitmap(BITMAP_WIFILINK_PRESSED_37X26_ID));	
		}
		WirelessConnection.invalidate();	//显示
}
//页面跳转
void WiFiPageView::WiFiPageChange(uint8_t newFiveKeyFunc)
{
		handleKeyEvent(newFiveKeyFunc);
}